package com.meitianhui.convenitenbuy.module.mine.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.mine.contract.SearchGuiderStoreContract;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.ConsumerAddress;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.SearchGuiderStoreActivity;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.store.model.StoreInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by amy on 2017/11/28.
 */

public class SearchGuiderStorePresenter implements SearchGuiderStoreContract.Presenter {

    private SearchGuiderStoreContract.View mView;
    private SearchGuiderStoreActivity mContext;

    public SearchGuiderStorePresenter(SearchGuiderStoreActivity activity,
                                      SearchGuiderStoreActivity context) {
        this.mView = activity;
        this.mContext = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getDefaultReceivingAdd() {
        Map<String, String> map = new HashMap<>();
        map.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());
        map.put("is_major_addr", "Y");

        MineApiDataSource.getInstance()
                .getUserReceivingAddress(InfoProvider.getAppToken(mContext),
                        "member.consumerAddressFind", new Gson().toJson(map))
                .subscribe(new ResponseObserver<ConsumerAddress>(mContext) {
                    @Override
                    public void onSuccess(ConsumerAddress consumerAddress) {
                        if (consumerAddress.data.size() > 0) {
                            // 已经设置了默认收货地址
                            mView.getDefaultReceivingAddSucc(true);
                        } else {
                            // 没有设置收货地址
                            mView.getDefaultReceivingAddSucc(false);
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

    @Override
    public void getDefualtStore() {
        Map<String, String> map = new HashMap<>();
        map.put("longitude", InfoProvider.getLongitude(mContext));
        map.put("latitude", InfoProvider.getLatitude(mContext));

        MineApiDataSource.getInstance()
                .getDefaultSearchStore(InfoProvider.getAppToken(mContext),
                        "member.nearbyAllStoreFindForHomePage", new Gson().toJson(map))
                .subscribe(new ResponseObserver<BaseResponse<StoreInfo>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<StoreInfo> response) {
                        List<StoreInfo.Convenient> list = response.data.getList();
                        if (list.size() > 0) {
                            mView.getDefaultStoreSucc(list);
                        } else {
                            // 为空
                            mView.showEmpty();
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

    /**
     * 申请去绑定店铺的guider
     */
    @Override
    public void bindStoreGuider(StoreInfo.Convenient convenient) {
        // 这里复用一下deleteshopGuider的请求网络框架，因为参数一样，不想再写一个一样的了
        Map<String, String> map = new HashMap<>();
        map.put("stores_id", convenient.getStores_id());
        map.put("stores_name", convenient.getStores_name());
        map.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());
        map.put("consumer_mobile", InfoProvider.getUserToken(mContext).getMobile());

        MineApiDataSource.getInstance()
                .deleteShopGuider(InfoProvider.getUserToken(mContext).getUser_token(),
                        "saleAssistant.consumer.saleAssistantApply", new Gson().toJson(map))
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(mContext) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.bindGuiderSucc();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);//
                    }
                });
    }

    /**
     * 获取搜索结果
     */
    @Override
    public void getSearchStores(String str) {
        Map<String, String> maps = new HashMap<>();
        maps.put("page_no", "1");
        maps.put("page_size", "20");
        Map<String, String> map = new HashMap<>();
        map.put("stores_like", str);

        MineApiDataSource.getInstance()
                .getSearchStores(InfoProvider.getAppToken(mContext),
                        "member.storeListPageFind", new Gson().toJson(maps), new Gson().toJson(map))
                .subscribe(new ResponseObserver<BaseResponse<StoreInfo>>(mContext) {
                    @Override
                    public void onSuccess(BaseResponse<StoreInfo> response) {
                        List<StoreInfo.Convenient> convenientList = response.data.getList();
                        if (convenientList.size() > 0) {
                            mView.getSearchStoresSucc(convenientList);
                        } else {
                            mView.showEmpty();
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

}
